|
|
|
|
|
|
|
As was mentioned earlier, this sort of transformation will not work for all Visual Basic drawing commands. The Circle method, for instance, cannot draw an arc that has been arbitrarily scaled and rotated. Similarly, you can use the Line statement to draw a rectangle but not a rotated rectangle. |
|
|
|
|
|
|
|
|
If you want to be able to apply general transformations to all your graphical objects, you cannot use Visual Basic commands that are hard to transform. Instead, use the Line command to draw arcs and other more complicated shapes using short line segments. Then you can transform the object by applying a transformation matrix to the points that make up the line segments. |
|
|
|
|
|
|
|
|
Because the TwoDPolygon object can hold any number of points, it makes a convenient tool for drawing other kinds of objects. For example, to draw a transformed rectangle, you can create a TwoDPolygon object that draws lines connecting the rectangle's four corners. To draw a circle, you can use a TwoDPolygon object holding a large number of points along the circle's edge. |
|
|
|
|
|
|
|
|
Example program RotRect, shown in Figure 12.21, demonstrates a greatly simplified TwoDPolygon class. This class provides none of the drawing attribute functions implemented by the previous version, but it does have a Transform routine. |
|
|
|
|
|
|
|
|
Enter an angle of rotation in program RotRect's text box. Then click and drag to define a rectangle. The program creates a TwoDPolygon object and initializes it using the points you selected. It then uses the m2RotateAround subroutine to create a transformation matrix that rotates around the rectangle's center. It applies the transformation using the Transform routine and displays the result. |
|
|
|
|
|
|
|
|
Shape-Distorting Transformations |
|
|
|
|
|
|
|
|
The transformations described so far have all been shape preserving. You can use scaling to stretch an object, but scaling stretches all the points in an object by an equal amount. While you can change the aspect ratio of an object, a straight line remains a straight line no matter what combination of translation, scaling, and rotation you apply to it. |
|
|
|
|
|
|
|
|
It is convenient to represent transformations as matrices, but there are other types of transformations that you cannot represent in this way. For example, you could transform the points in an object using the following equations: |
|
|
|
 |
|
|
|
|
new_x = old_x + Amplitude * Sin(old_y * 6.28 / Period)
new_y = old_y + Amplitude * Sin(old_x * 6.28 / Period) |
|
|
|
|
|
|
|
|
In this transformation, the new X coordinate depends on the sine of the Y coordinate, and the new Y coordinate depends on the sine of the X coordinate. Because multiplying a vector and a matrix cannot produce the sine of a number, you cannot use matrices to represent this transformation. Figure 12.22 shows a grid and three shape-distorting |
|
|
|
|
|